package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.InvoiceOrderDao;
import lxt.entity.InvoiceOrder;
/**
 * 订单dao
 * @author lit
 *
 */
@SuppressWarnings("unchecked")
public class InvoiceOrderDaoImpl extends ABseDaoImpl implements InvoiceOrderDao {
	/**
	 * 分页查询
	 */
	public List<InvoiceOrder> loadByPage(String orderTime, String ClientNameS,
			String ClientNameR, int offset, Integer pageSize) {
		StringBuffer hql = new StringBuffer("select i from InvoiceOrder i where 1 = 1");
		ArrayList<Object> param = new ArrayList<Object>();
		if(orderTime != null && !"".equals(orderTime)){
			hql.append("And i.orderId like ?");
			param.add("%"+orderTime+"%");
		}
		if(ClientNameS != null && !"".equals(ClientNameS)){
			hql.append("And i.clientInfoBySClient.clientName like ?");
			param.add("%" + ClientNameS + "%");
		}
		if(ClientNameR != null && !"".equals(ClientNameR)){
			hql.append("And i.clientInfoByRClient.clientName like ?");
			param.add("%" + ClientNameR + "%");
		}
		return (List<InvoiceOrder>)this.queryForPage(hql.toString(), param, offset, pageSize);
	}
	
	/**
	 * 查询总条数
	 */
	public Integer loadOrderCount(String orderTime, String ClientNameS,
			String ClientNameR) {
		StringBuffer hql = new StringBuffer("select i from InvoiceOrder i where 1 = 1");
		ArrayList<Object> param = new ArrayList<Object>();
		if(orderTime != null && !"".equals(orderTime)){
			hql.append("And i.orderId like ?");
			param.add("%"+orderTime+"%");
		}
		if(ClientNameS != null && !"".equals(ClientNameS)){
			hql.append("And i.clientInfoBySClient.clientName like ?");
			param.add("%" + ClientNameS + "%");
		}
		if(ClientNameR != null && !"".equals(ClientNameR)){
			hql.append("And i.clientInfoByRClient.clientName like ?");
			param.add("%" + ClientNameR + "%");
		}
		return this.getAllRowCount(hql.toString(), param);
	}
	
	/**
	 * 根据格式化的日期查询订单ID
	 * @param date
	 * @return
	 */
	public List<String> queryOrderId(String date){
		StringBuffer hql = new StringBuffer("Select a.orderId from InvoiceOrder a where 1=1 ");
		ArrayList<Object> param = new ArrayList<Object>();
		List<String> list = null ;
		if(date!=null&&!date.equals("")){
			hql.append(" And a.orderId like ? order by a.orderId asc ");
			param.add("%"+date+"%");
		}
		list = this.getHibernateTemplate().find(hql.toString(),param.toArray());
		return list;
	}
	/**
	 * 根据发货时间查询订单id 派单查询
	 * @return
	 */
	public List<String> queryInvosOrderId(String order,int offset, int length){
		StringBuffer hql = new StringBuffer("Select a.orderId from InvoiceOrder a where a.STime is null ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(order!=null&&!order.equals("")){
			hql.append(" And a.orderId like ? ");
			param.add("%"+order+"%");
		}
		return (List<String>) this.queryForPage(hql.toString(), param, offset, length);
	}
	/**
	 * 查询数量
	 */
	public int loadInvosOrderIdCount(String order){
		StringBuffer hql = new StringBuffer("Select a.orderId from InvoiceOrder a where a.STime is null ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(order!=null&&!order.equals("")){
			hql.append(" And a.orderId like ? ");
			param.add("%"+order+"%");
		}
		return this.getAllRowCount(hql.toString(), param);
	}
}
